using System;
using System.Collections.Generic;
using CommunityToolkit.Mvvm.ComponentModel;
using MQTTnet;

namespace AvaloniaApplication1.Models;

public class MqttDeviceModel
{
    /// <summary>
    /// MQTT设备信息模型
    /// </summary>
    /// <summary>
    /// 设备ID
    /// </summary>
    public string DeviceId { get; set; }

    /// <summary>
    /// 服务器地址
    /// </summary>
    public string Server { get; set; }

    /// <summary>
    /// 端口
    /// </summary>
    public int Port { get; set; }

    /// <summary>
    /// 用户名
    /// </summary>
    public string Username { get; set; }

    /// <summary>
    /// 密码
    /// </summary>
    public string Password { get; set; }

    // /// <summary>
    // /// 订阅主题
    // /// </summary>
    // public string Topic { get; set; }
    //
    // /// <summary>
    // /// QoS等级
    // /// </summary>
    // public int Qos { get; set; }

    /// <summary>
    /// MQTT客户端实例
    /// </summary>
    public required IMqttClient MqttClient { get; set; }

    /// <summary>
    /// 连接状态
    /// </summary>

    public bool IsConnected { get; set; }

    /// <summary>
    /// 连接时间
    /// </summary>
    public DateTime ConnectedTime { get; set; }

    /// <summary>
    /// 最后活动时间
    /// </summary>
    public DateTime LastActivityTime { get; set; }

    /// <summary>
    /// 设备描述
    /// </summary>
    public string Description { get; set; }

    public MqttDeviceModel(string deviceId, string server, int port, string username, string password,
        List<MqttTopicConfig> topics, bool isConnected)
    {
        DeviceId = deviceId;
        Server = server;
        Port = port;
        Username = username;
        Password = password;
        Topics = topics;
        IsConnected = isConnected;
        Description = $"Device {deviceId} ({server}:{port})";
    }

    /// <summary>
    /// 主题配置列表（包含订阅和发布的完整配置）
    /// </summary>
    public List<MqttTopicConfig> Topics { get; set; } = new();
}

/// <summary>
/// 主题配置模型
/// </summary>
public class MqttTopicConfig
{
    /// <summary>
    /// 主题
    /// </summary>
    public string Topic { get; set; }

    /// <summary>
    /// QoS等级
    /// </summary>
    public int Qos { get; set; }

    /// <summary>
    /// 是否为接收消息的主题
    /// </summary>
    public bool IsSubscription { get; set; }

    /// <summary>
    /// 是否为发送消息的主题
    /// </summary>
    public bool IsPublish { get; set; }

    public  event Action<MqttTopicConfig>? DeleteRequested;


    // 添加删除和编辑命令方法
    public void RequestDelete()
    {
        DeleteRequested?.Invoke(this);
    }
    

    // 添加构造函数
    public MqttTopicConfig()
    {
        // 使用默认值
    }

    public MqttTopicConfig(string topic, int qos =0, bool isSubscription = true, bool isPublish = false)
    {
        Topic = topic;
        Qos = qos;
        IsSubscription = isSubscription;
        IsPublish = isPublish;
    }
}